package com.maidou.qiantai.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.maidou.qiantai.domain.Member;
import com.maidou.qiantai.domain.Spu;
import com.maidou.qiantai.service.IMemberService;
import com.maidou.qiantai.service.ISpuService;
import com.maidou.qiantai.vo.SpuCommentVo;
import com.maidou.qiantai.vo.SpuInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.maidou.qiantai.domain.SpuComment;
import com.maidou.qiantai.service.ISpuCommentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品评论Controller
 * 
 * @author maidou
 * @date 2024-03-21
 */
@RestController
@RequestMapping("/qiantai/comment")
public class SpuCommentController extends BaseController
{
    @Autowired
    private ISpuCommentService spuCommentService;
    @Resource
    private ISpuService spuService;
    @Resource
    private IMemberService memberService;

    /**
     * 查询商品评论列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(SpuComment spuComment)
    {
        startPage();
        List<SpuComment> list = spuCommentService.selectSpuCommentList(spuComment);
        List<SpuCommentVo> collect = list.stream().map(item -> {
            SpuCommentVo spuCommentVo = new SpuCommentVo();
            BeanUtils.copyProperties(item, spuCommentVo);
            Member member = memberService.selectMemberById(item.getMemberId());
            spuCommentVo.setMember(member);
            Spu spu = spuService.selectSpuBySpuId(item.getSpuId());
            spuCommentVo.setSpu(spu);
            return spuCommentVo;
        }).collect(Collectors.toList());
        long total = new PageInfo<>(list).getTotal();
        TableDataInfo dataTable = getDataTable(collect);
        dataTable.setTotal(total);
        return dataTable;
    }

    /**
     * 导出商品评论列表
     */
    @PreAuthorize("@ss.hasPermi('qiantai:comment:export')")
    @Log(title = "商品评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SpuComment spuComment)
    {
        List<SpuComment> list = spuCommentService.selectSpuCommentList(spuComment);
        ExcelUtil<SpuComment> util = new ExcelUtil<SpuComment>(SpuComment.class);
        util.exportExcel(response, list, "商品评论数据");
    }

    /**
     * 获取商品评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('qiantai:comment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(spuCommentService.selectSpuCommentById(id));
    }

    /**
     * 新增商品评论
     */
    @PreAuthorize("@ss.hasPermi('qiantai:comment:add')")
    @Log(title = "商品评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SpuComment spuComment)
    {
        return toAjax(spuCommentService.insertSpuComment(spuComment));
    }

    /**
     * 修改商品评论
     */
    @PreAuthorize("@ss.hasPermi('qiantai:comment:edit')")
    @Log(title = "商品评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SpuComment spuComment)
    {
        return toAjax(spuCommentService.updateSpuComment(spuComment));
    }

    /**
     * 删除商品评论
     */
    @PreAuthorize("@ss.hasPermi('qiantai:comment:remove')")
    @Log(title = "商品评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(spuCommentService.deleteSpuCommentByIds(ids));
    }
}
